package audio;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class AudioAnaly {
		 FileInputStream file ;
		 BufferedInputStream file1;
		public AudioAnaly(String filename) throws FileNotFoundException{
			file = new FileInputStream(filename);
			file1 = new BufferedInputStream(file); 
		}
		public double[] shiyu() throws IOException
		{
				//file = new FileInputStream("bulage1.wav");
				//bis = new BufferedInputStream(file); 
				
				
			String ChunkID = readString(4);  
			if(!ChunkID.endsWith("RIFF"))  
				throw new RuntimeException("The file is not an RIFF file");  
			System.out.printf("%s\n",ChunkID);
			int Chunksize = readLong(); 			
			System.out.println(Chunksize);
			
			String format = readString(4);
			if(!format.endsWith("WAVE"))
				throw new RuntimeException("The file is not an WAVE file");
			System.out.printf("%s\n",format);
			for(int i=0; i<8; ++i){  
				System.out.print(file1.read());  }
			
			System.out.println();			
			int audioformat = readInt(); 
			if(audioformat!=1)
				throw new RuntimeException("The file is not an PCM file");
			System.out.println(audioformat);
			
			int numchannels = readInt(); 
			if(numchannels!=1)
				throw new RuntimeException("The audio channel > 1");
			System.out.println(numchannels);
	
			int samplerate = readLong(); 
			if(samplerate!=44100)
				throw new RuntimeException("The audio's sample rate is not 44100 Hz");
			System.out.println(samplerate);
			
			
			for(int i=0; i<6; ++i){  
				System.out.print(file1.read());  }
			
			int bitspersample = readInt();
			if(bitspersample!=16)
				throw new RuntimeException("The audio's bit depth is not 16-bit");
			System.out.println(bitspersample);
				
				short shiyu1[] = new short[(Chunksize-38)/2];
				for(int i=0 ;i<(Chunksize-38)/2;i++)
				{
					shiyu1[i]=readShort();
				}
				double [] shiyu2 = new double[(Chunksize-38)/2];
				for(int i=0 ;i<(Chunksize-38)/2;i++)
				{
					shiyu2[i]=shiyu1[i];
				}
			 		
				
			
			    return shiyu2;
			
			
		}
		 private String readString(int len){  
		        byte[] buf = new byte[len];  
		        try {  
		            if(file1.read(buf)!=len)  
		                throw new IOException("no more data!!!");  
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		        return new String(buf);  
		 }
		 private  int readInt(){  
		        byte[] buf = new byte[2];  
		        int res = 0;  
		        try {  
		            if(file1.read(buf)!=2)  
		                throw new IOException("no more data");  
		            res = (buf[0]&0x000000FF) | (((int)buf[1])<<8);  
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		        return res;  
		 }
		 
		  
		    private  int readLong(){  
		        int res = 0;  
		        try {  
		            int[] l = new int[4];  
		            for(int i=0; i<4; ++i){  
		                l[i] = file1.read();  
		                if(l[i]==-1){  
		                    throw new IOException("no more data");  
		                }  
		            }  
		            res = l[0] | (l[1]<<8) | (l[2]<<16) | (l[3]<<24);  
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		        return res;  
		    } 
		    
		    private  short readShort(){  
		    	short res =0;
		    	byte[] buf = new byte[2];  
		    	try {  
		            if(file1.read(buf)!=2)  
		                throw new IOException("no more data"); 
		             res = (short) (buf[0] | (((short)buf[1])<<8));                  	            
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		        return res;   
		    } 
	

}
